package cn.edu.xaut.servlet;

import cn.edu.xaut.Dao.GoodsDao;
import cn.edu.xaut.bean.Goods;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "GoodsMarket", urlPatterns = "/goodsmarket")
public class ShowGoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        resp.setContentType( "text/html;charset=utf-8" );
        resp.setCharacterEncoding( "utf-8" );
        req.setCharacterEncoding( "utf-8" );

        if (req.getSession().getAttribute("user") == null) {
            resp.getWriter().println("请先登录,<a href='login.jsp'>返回登录</a>");
            return;
        }

        //查询数据库
        GoodsDao goodsDao = new GoodsDao();
        try{
            List<Goods> goodslist= goodsDao.findAll();
            req.setAttribute("goodsList", goodslist);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }
        req.getRequestDispatcher( "/goods_buy.jsp" ).forward( req,resp );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
